package giro.general.team;

import giro.general.ListBaseActivity;
import giro.general.R;
import giro.general.adapter.SquadreArrayAdapter;
import giro.general.util.Commons;
import giro.general.util.Parser;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class Squadre extends ListBaseActivity {
	private JSONArray squadreArray = null;
	private String[] squadre = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.squadre);
		initToolbar();
		squadreArray = Parser.getInstance().createArrayJson(this, R.raw.squadre);
		Commons.getInstance().setSquadreArray(squadreArray);

		try {
			Parser.getInstance().parse(Commons.getInstance().getsquadreArray(), "squadra");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		squadre = Commons.getInstance().getSquadre();

		SquadreArrayAdapter adapter = new SquadreArrayAdapter(this, squadre);
		setListAdapter(adapter);

		final ListView lv = getListView();
		lv.setTextFilterEnabled(true);

		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//				postToast(getResources().getString(R.string.Pending_TeamDetail));

				Intent intent = new Intent(getApplicationContext(), DettagliSquadra.class);
				String pkg = getPackageName();
				Object ob = new Object();
				ob = lv.getItemAtPosition(position);
				intent.putExtra(pkg + ".squadra", ob.toString());

				startActivity(intent);

			}
		});

	}

	/*
	 * public void creaArrayJson() {
	 * 
	 * InputStream is = getResources().openRawResource(R.raw.squadre); Writer
	 * writer = new StringWriter(); char[] buffer = new char[1024]; try { Reader
	 * reader = null; reader = new BufferedReader(new InputStreamReader(is,
	 * "UTF-8")); int n; while ((n = reader.read(buffer)) != -1) {
	 * writer.write(buffer, 0, n); } } catch (IOException e) {
	 * e.printStackTrace(); } finally { try { is.close(); } catch (IOException
	 * e) { e.printStackTrace(); } }
	 * 
	 * try { squadreArray = new JSONArray(writer.toString()); } catch
	 * (JSONException e) { e.printStackTrace(); }
	 * 
	 * Commons.getInstance().setSquadreArray(squadreArray);
	 * 
	 * }
	 */

}
